package com.liyuxiang.yygh.cmn.controller;

import com.liyuxiang.yygh.cmn.service.DictService;
import com.liyuxiang.yygh.common.result.Result;
import com.liyuxiang.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/admin/cmn/dict")
@Api(tags = "数据字典")
public class DictController {
    @Autowired
    DictService dictService;

    @ApiOperation(value = "根据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable("id") Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    //导出数据字典接口
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    @PostMapping("importData")
    public void importData(MultipartFile file) {
        dictService.importData(file);
    }

    @GetMapping("getName/{dictCode}/{value}")
    public String getDictName(@PathVariable("dictCode") String dictCode, @PathVariable("value") String value) {
        String dictName = dictService.getDictName(dictCode, value);
        return dictName;
    }

    @GetMapping("getName/{value}")
    public String getDictName(@PathVariable("value") String value) {
        String dictName = dictService.getDictName("", value);
        return dictName;
    }
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable("dictCode") String dictCode){
        List<Dict> dictList = dictService.findByDictCode(dictCode);
        return Result.ok(dictList);
    }


}
